//
//  PaintingPageViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/24.
//

import UIKit
import JXSegmentedView

class PaintingPageViewController: UIViewController {
    let disposeBag = DisposeBag()
    lazy var segmentedDataSource:JXSegmentedTitleImageDataSource = {
        let dataSource = JXSegmentedTitleImageDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.titleImageType = .onlyTitle
        dataSource.titleNormalColor = Theme.color.text_d3
        dataSource.titleSelectedColor = .white
        dataSource.titleNormalFont = UIFont.pingFangSCSemibold(ofSize: 16)
        dataSource.titleSelectedFont = UIFont.pingFangSCSemibold(ofSize: 16)
//        dataSource.isItemSpacingAverageEnabled = true
        let width = (SCREEN_WIDTH - 32) / 2
        dataSource.itemWidth = width
        dataSource.itemSpacing = 0
        dataSource.titles = ["生成绘画", "绘画广场"]
        return dataSource
    }()
    
    fileprivate lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hexString: "#DEE0E9")
        view.allRound(23)
        return view
    }()
    
    lazy var segmentedView:JXSegmentedView = {
        let segmentedView = JXSegmentedView()
        segmentedView.defaultSelectedIndex = 0
        segmentedView.backgroundColor = .clear
        segmentedView.listContainer = listContainerView
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
        indicator.indicatorHeight = 46
        indicator.indicatorColor = UIColor(hexString: "#45DFFF")
        segmentedView.indicators = [indicator]
        return segmentedView
    }()
    
    
    lazy var listContainerView: JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.view.addSubviews([backgroundView, listContainerView])
        backgroundView.addSubview(segmentedView)
        backgroundView.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop).offset(10)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(46)
        }
        
        segmentedView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        listContainerView.snp.makeConstraints { make in
            make.top.equalTo(backgroundView.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(self.safeBottom)
        }
        GlobalRx.jumpDrawSubject.observe(on: MainScheduler.instance).subscribe(onNext: { [weak self] in
//            self?.tabBarController?.selectedIndex = 0
            self?.segmentedView.selectItemAt(index: 0)
        }).disposed(by: disposeBag)
    }
    
    func initUI(){
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        
//        let leftBtn = UIButton(type: .custom)
//        leftBtn.setImage(UIImage(named: "icon_home_left"), for: .normal)
//        let leftItem = UIBarButtonItem(customView: leftBtn)
//        self.navigationItem.leftBarButtonItem = leftItem
//        
        
        self.navigationItem.title = "绘画"
        let rightBtn = UIButton(type: .custom)
        rightBtn.setImage(UIImage(named: "icon_home_sub"), for: .normal)
        rightBtn.backgroundColor = UIColor(hexString: "#464957")
        rightBtn.setTitle("订阅", for: .normal)
        rightBtn.allRound(12)
        rightBtn.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 15)
        rightBtn.layoutButtonWithEdgeInsetsStyle(.Left, space: 5)
        rightBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 12, bottom: 3, right: 12)
        let rightItem = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightItem
        
        rightBtn.setClosureTouchUpInside_k { [weak self] in
            if Appearance.shared.accoutModel != nil {
                let vc = SubscribeViewController()
                self?.navigationController?.pushViewController(vc, animated: true)
            }else {
                GlobalRx.loginSubject.onNext(())
            }
        }
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews() 
        if let segmentedIndicator = segmentedView.indicators.first as? JXSegmentedIndicatorBackgroundView {
            let backgroundViewWidth = backgroundView.bounds.width
            let indicatorWidth = backgroundViewWidth / 2
            segmentedIndicator.indicatorWidth = indicatorWidth
            segmentedView.reloadData()
        }
    }
}
extension PaintingPageViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            return titleDataSource.dataSource.count
        }
        return 0
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 1 {
            return PaintingListViewController()
        }
        let vc = PaintingViewController()
        return vc
    }
    
    
}
